



package wviz.gui;

import javax.swing.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.Clipboard;
import java.awt.event.InputEvent;
import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.File;
import java.net.URI;

/**
 * Utility class that turns drag-and-drop events into FileDragAndDropCallback callback.
 * <p>
 * Copied from Tika project
 * <p>
 * @author Mohammad Asgari
 * @version 1.0
 * @date 11-Dec-2008
 */
class ParsingTransferHandler extends TransferHandler {

    private final TransferHandler delegate;

    private final FileDragAndDropCallback dragAndDropCallback;//visualisation application for callbacks

    private static DataFlavor uriListFlavor;

    static {
         try {
             uriListFlavor = new DataFlavor("text/uri-list;class=java.lang.String");
         } catch (ClassNotFoundException e) {
         }
    }

    public ParsingTransferHandler(TransferHandler delegate, FileDragAndDropCallback dragAndDropCallback) {
        this.delegate = delegate;
        this.dragAndDropCallback = dragAndDropCallback;
    }

    public boolean canImport(JComponent component, DataFlavor[] flavors) {
        for (DataFlavor flavor : flavors) {
            if (flavor.equals(DataFlavor.javaFileListFlavor) || flavor.equals(uriListFlavor)) {
                return true;
            }
        }
        return false;
    }

    public boolean importData(
            JComponent component, Transferable transferable) {
        try {
            List<?> files = null;
            if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                files = (List<?>)
                    transferable.getTransferData(DataFlavor.javaFileListFlavor);
            } else if (transferable.isDataFlavorSupported(uriListFlavor)) {
                files = uriToFileList((String) transferable.getTransferData(uriListFlavor));
            }
            if (files == null)
                return false;

            for (Object file : files) {
                dragAndDropCallback.importFile((File) file);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public void exportAsDrag(JComponent arg0, InputEvent arg1, int arg2) {
        delegate.exportAsDrag(arg0, arg1, arg2);
    }

    public void exportToClipboard(JComponent arg0, Clipboard arg1, int arg2)
            throws IllegalStateException {
        delegate.exportToClipboard(arg0, arg1, arg2);
    }

    public int getSourceActions(JComponent arg0) {
        return delegate.getSourceActions(arg0);
    }

    public Icon getVisualRepresentation(Transferable arg0) {
        return delegate.getVisualRepresentation(arg0);
    }

    private static List<File> uriToFileList(String data) {
        List<File> list = new ArrayList<File>();
        StringTokenizer st = new StringTokenizer(data, "\r\n");
        while (st.hasMoreTokens())
        {
            String s = st.nextToken();
            if (s.startsWith("#")) {
                continue;
            }
            try {
                list.add(new File(new URI(s)));
            } catch (Exception e) {
            }
        }
        return list;
    }
}

